<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2024-06-12 14:03:51
 */

namespace app\mobmall_manage\model;
use app\common\model\BaseModel;

class User extends BaseModel {
	// 设置数据表（不含前缀）
	protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';
	
	// 自动完成
	protected $auto       = ['last_login_ip'];
	protected $insert     = ['register_ip'];

	// 定义排序字段
	protected $_orderField = 'create_time';
	protected $_orderDesc = 'desc';

	public function setRegisterIpAttr($value) {
		return request()->ip();
	}

	public function setPasswordAttr($value) {
		if (!empty($value)) {
			return sp_password($value); //encryptAdmin
		}
	}

	private function getStatusTextAttr($value) {
		$statusArr = [1 => '正常', 2 => '待审核', 3 => '禁止'];
		return isset($statusArr[$value]) ? $statusArr[$value] : '未知';
	}

	private function getSexTextAttr($value) {
		$sexArr = [0 => '保密', 1 => '男', 2 => '女'];
		return isset($sexArr[$value]) ? $sexArr[$value] : '未知';
	}

	/**
	 * 获取注册的时间戳
	 * @param  [type] $value [description]
	 * @param  [type] $data [description]
	 * @return [type] [description]
	 * @date   2018-02-28
	 * @author 心云间、凝听 <981248356@qq.com>
	 */
	// public function getRegTimestampAttr($value,$data) {
	// 	$timestamp = $data['reg_time'];
	// 	//判断是否是时间戳
	// 	// if(strtotime(date('m-d-Y H:i:s',$timestamp)) != $timestamp) {
	// 	//     $timestamp = strtotime($timestamp);
	// 	// }

	// 	return $timestamp;
	// }

	public function checkByUsername($username) {
		$map = array(
			'username'	=> $username,
		);
		$info = $this->where($map)->find();
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function checkByMobile($mobile) {
		$map = array(
			'mobile'	=> $mobile,
		);
		$info = $this->where($map)->find();
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit='', $orderBy='') {
		if (!$orderBy) {
			$orderBy = [$this->_orderField=>$this->_orderDesc];
		}
		$data = $this->where($map)->order($orderBy)->limit($limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');
		$yes_config = config('engineeconfig.yes_config');
		$gender_config = config('gender_config');
		$site_domain = config('system.site_domain');

		$list = $uids = $shids = array();
		foreach ($data as $key => $value) {
			//通用解析
			//$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			//$value['status_show'] = isset($value['status']) ? $isopen_config[$value['status']] : '';
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//通用解析

			$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			$value['last_login_time_show'] = $value['last_login_time'] ? date("Y-m-d H:i:s", $value['last_login_time']) : '';

			$value['status_show'] = $this->getStatusTextAttr($value['status']);

			if ($value['unionid']) { //微信用户

			} else { //手机用户
				//$value['avatar'] = $value['avatar'] ? $ftpWeb.$value['avatar'] : '';
			}
			
			if (!$value['avatar']) {
				$value['avatar'] = $site_domain.'/static/danceui/images/avatar/avatar-default.png';
			}
			if ($value['avatarimg']) {
				$value['avatar'] = $ftpWeb.$value['avatarimg'];
			}
			
			$value['gender_show'] = $value['gender'] ? $gender_config[$value['gender']] : '';
			$value['is_creditacc_show'] = isset($value['is_creditacc']) ? $yes_config[$value['is_creditacc']] : '';

			$value['onlinetime_show'] = $value['onlinetime'] ? sec2Time($value['onlinetime']) : '';

			$list[$value['id']] = $value;
			$uids[$value['id']] = $value['id'];
			$shids[$value['shid']] = $value['shid'];
		}
		return array($list, $uids, $shids);
	}

	protected function parseInfo($info) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');
		$gender_config = config('gender_config');
		$site_domain = config('system.site_domain');

		//通用解析
		//$info['create_time_show'] = $info['create_time'] ? date("Y-m-d H:i:s", $info['create_time']) : '';
		//$info['status_show'] = isset($info['status']) ? $isopen_config[$info['status']] : '';
		//$info['picurl'] = $info['picurl'] ? $ftpWeb.$info['picurl'] : '';
		//通用解析

		$info['status_show'] = $this->getStatusTextAttr($info['status']);

		if ($info['unionid']) { //微信用户

		} else { //手机用户
			//$info['avatar'] = $info['avatar'] ? $ftpWeb.$info['avatar'] : '';
		}
		
		if (!$info['avatar']) {
			$info['avatar'] = $site_domain.'/static/danceui/avatar/none.png';
		}
		if ($info['avatarimg']) {
			$info['avatar'] = $ftpWeb.$info['avatarimg'];
			unset($info['avatarimg']);
		}
		
		$info['gender_show'] = $info['gender'] ? $gender_config[$info['gender']] : '';

		$info['onlinetime_show'] = $info['onlinetime'] ? sec2Time($info['onlinetime']) : '';
	
		#$info['mobile_asterisk'] = substr($info['mobile'], 0, 3)."****".substr($info['mobile'], 8, 4);
		$info['mobile_asterisk'] = $info['mobile'];

		return $info;
	}

	public function getInfoByOpenid($openid) {
		$map = [
			['openid', '=', $openid]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByUnionid($unionid) {
		$map = [
			['unionid', '=', $unionid]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByUsername($username) {
		$map = [
			['username', '=', $username]
		];
		$info = $this->where($map)->find();
		//echo $this->getLastSql();exit;
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByMobile($mobile) {
		$map = [
			['mobile', '=', $mobile]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByEmail($email) {
		$map = [
			['email', '=', $email]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}
	
	public function getInfoByPcOpenid($openidpcweixin) {
		$map = [
			['openidpcweixin', '=', $openidpcweixin]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByAppleOpenid($appleOpenid) {
		$map = [
			['apple_openid', '=', $appleOpenid]
		];
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

// 	public function assembleData($info) {
// 		$retInfo = [
// 			'uid'				=> $info['uid'],
// 			'username'			=> $info['username'],
// 			'unionid'			=> $info['unionid'],
// 			'avatar'			=> $info['avatar'],
// 			'comp_name'			=> $info['comp_name'], //公司名称
// 			'mobile_asterisk'	=> $info['mobile_asterisk'], //手机号
// 			'realbalance'		=> $info['realbalance'], //余额
// 			'realintegral'		=> $info['realintegral'], //积分
// 			'is_creditacc'		=> $info['is_creditacc'], //信用结算账户
// 			'collect_num'		=> $info['collect_num'],
// 			'follow_num' 		=> $info['follow_num'],
// 			'evaluation_num' 	=> $info['evaluation_num'],
// 			'coupon_num'		=> $info['coupon_num'],
// 			'redpacket_num'		=> $info['redpacket_num'],
// 			'browse_num'		=> $info['browse_num'],
// 			'status_show'		=> $info['status_show'],
// 			'shid'				=> $info['shid'],
// 			'needUserinfo'		=> $needUserinfo
// 		];
// 		return $retInfo;
// 	}
	
}